using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Data.Service;
using ToDo.Models;
using System.Collections.Generic;
using ToDoList.Data;


namespace ToDo.Controllers
{
    public class UserController : Controller
    {
        private UserService _userService = new UserService();
        private InventoryService _inventoryService = new InventoryService();
        private ItemService _itemService = new ItemService();

        /// <summary>
        /// 功能列表
        /// </summary>
        /// <returns></returns>
        public IActionResult StartToLogin()
        {
            if (HttpContext.Session.Get("userId") != null)
            {
                //登录后的清单界面
                return View("ShowInventory", "Inventory");
            }
            else
                return View("Login");
        }

        [HttpPost]
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="id"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public IActionResult Login(string userName, string password)
        {
            Users result = _userService.Login(userName, password);
            //登录成功
            if (result != null)
            {
                HttpContext.Session.SetInt32("userId", result.Id);
                _itemService.OverTime(result.Id);//将已过期事项改变状态
                return RedirectToAction("ShowInventory", "Inventory");
            }
            else
                return Json("fail");
        }

        /// <summary>
        /// 将Inventory集合转换成InventoryViewModel集合
        /// </summary>
        /// <param name="inventoryList"></param>
        /// <returns></returns>
        public List<InventoryViewModel> SaveInViewModels(List<Inventorys> inventoryList)
        {
            List<InventoryViewModel> viewInventoryList = new List<InventoryViewModel>();
            foreach (var inventory in inventoryList)
            {
                var Inventory = new InventoryViewModel()
                {
                    InventoryName = inventory.InventoryName,
                    CreateTime = inventory.CreateTime,
                    InventoryId = inventory.InventoryId,
                    UserId = inventory.UserId,
                };
                viewInventoryList.Add(Inventory);
            }
            return viewInventoryList;
        }

        /// <summary>
        /// 展示注册界面
        /// </summary>
        /// <returns></returns>
        public IActionResult ToRegister()
        {
            return View("Register");
        }

        [HttpPost]
        /// <summary>
        /// 注册
        /// </summary>
        /// <returns></returns>
        public IActionResult Register(string userName, string password, string repassword)
        {
            var result = _userService.Register(userName, password, repassword);
            if (result)
                return View("Login");
            return Json("fail");
        }
        /// <summary>
        /// 注销
        /// </summary>
        /// <returns></returns>
        public IActionResult Logout()
        {
            HttpContext.Session.Remove("userId");
            return View("Login");
        }


        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="newPassword"></param>
        /// <returns></returns>
        public IActionResult ModifyPassword(string oldPassword, string newPassword, string againNewPassWord)
        {
            int userId = (int)HttpContext.Session.GetInt32("userId");
            var user = _userService.FindUser(userId);
            var userView = SaveUsersToUserViewModel(user);
            if (oldPassword == user.Password && newPassword == againNewPassWord)
            {
                _userService.ModifyUserPassWord(userId, newPassword);
                return View("UserInformationPage", userView);
            }
            else
                return View("ErrorPopupOfPassword");

        }
        /// <summary>
        /// 新建的清单名重复错误提示
        /// </summary>
        /// <returns></returns>
        public IActionResult ViewErrorPopup()
        {
            return View("ErrorPopupOfName");
        }
        /// <summary>
        /// 展示修改密码界面
        /// </summary>
        /// <returns></returns>
        public IActionResult GetModifyPasswordPage()
        {
            return View("ModifyPasswordPage");
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="newUserName"></param>
        /// <returns></returns>
        public IActionResult ModifyUserInformation(string newUserName, string newEmail)
        {
            int userId = (int)HttpContext.Session.GetInt32("userId");
            var user = _userService.FindUser(userId);
            if (newUserName == null)
            {
                newUserName = user.UserName;
            }
            if (newEmail == null)
            {
                newEmail = user.EMail;
            }
            _userService.ModifyUserInformation(userId, newUserName, newEmail);
            return RedirectToAction("UserInformationPage");
        }

        /// <summary>
        /// 修改密码界面
        /// </summary>
        /// <returns></returns>
        public IActionResult ModifyPasswordPage()
        {
            return View();
        }

        /// <summary>
        /// 用户信息界面
        /// </summary>
        /// <returns></returns>
        public IActionResult UserInformationPage()
        {
            var user = _userService.FindUser((int)HttpContext.Session.GetInt32("userId"));
            var userView = SaveUsersToUserViewModel(user);
            return View(userView);
        }

        /// <summary>
        /// 将Users转换成UserViewModel
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public UserViewModel SaveUsersToUserViewModel(Users user)
        {
            UserViewModel userView = new UserViewModel() {
                UserName = user.UserName,
                Id=user.Id,
                Email=user.EMail,
                Sex=user.Sex,
            };
            return userView;
        }
    }
}